package org.apache.ibatis.common.reflection.wrapper;

import org.apache.ibatis.common.reflection.MetaObject;
import org.apache.ibatis.common.reflection.ReflectorException;
import org.apache.ibatis.common.reflection.property.PropertyTokenizer;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @Name BaseWrapper
 * @Description: 对象（集合）包装器 [抽象] 基类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-28 15:13:05
 **/
public abstract class BaseWrapper implements ObjectWrapper {
    // 如果获取的对象属性没有参数，则使用它作为无参结果
    protected static final Object[] NO_ARGUMENTS = new Object[0];
    // 被包装对象的 “元” 对象
    protected final MetaObject metaObject;

    protected BaseWrapper(MetaObject metaObject) {
        this.metaObject = metaObject;
    }

    protected Object resolveCollection(PropertyTokenizer prop, Object collection) {
        if ("".equals(prop.getName())) return collection;
        else return metaObject.getValue(prop.getName());
    }

    protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
        if (collection instanceof Map) return (Map) ((Map<?, ?>) collection).get(prop.getIndex());
        else {
            int index = Integer.parseInt(prop.getIndex());
            if (collection instanceof List) return ((List) collection).get(index);
            else if (collection instanceof Object[]) return ((Object[]) collection)[index];
            else if (collection instanceof byte[]) return ((byte[]) collection)[index];
            else if (collection instanceof short[]) return ((short[]) collection)[index];
            else if (collection instanceof int[]) return ((int[]) collection)[index];
            else if (collection instanceof long[]) return ((long[]) collection)[index];
            else if (collection instanceof float[]) return ((float[]) collection)[index];
            else if (collection instanceof double[]) return ((double[]) collection)[index];
            else if (collection instanceof char[]) return ((char[]) collection)[index];
            else if (collection instanceof boolean[]) return ((boolean[]) collection)[index];
            else {
                String errorMsg = "容器 [" + collection + "] 的属性 <" + prop.getName() + "> 不是一个 List 或 Array。";
                throw new ReflectorException(errorMsg);
            }
        }
    }

    protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
        if (collection instanceof Map) ((Map) collection).put(prop.getIndex(), value);
        else {
            int index = Integer.parseInt(prop.getIndex());
            if (collection instanceof List) ((List) collection).set(index, value);
            else if (collection instanceof Object[]) ((Object[]) collection)[index] = value;
            else if (collection instanceof byte[]) ((byte[]) collection)[index] = (Byte) value;
            else if (collection instanceof short[]) ((short[]) collection)[index] = (Short) value;
            else if (collection instanceof int[]) ((int[]) collection)[index] = (Integer) value;
            else if (collection instanceof long[]) ((long[]) collection)[index] = (Long) value;
            else if (collection instanceof float[]) ((float[]) collection)[index] = (Float) value;
            else if (collection instanceof double[]) ((double[]) collection)[index] = (Double) value;
            else if (collection instanceof char[]) ((char[]) collection)[index] = (Character) value;
            else if (collection instanceof boolean[]) ((boolean[]) collection)[index] = (Boolean) value;
            else {
                String errorMsg = "容器 [" + collection + "] 的属性 <" + prop.getName() + "> 不是一个 List 或 Array。";
                throw new ReflectorException(errorMsg);
            }
        }
    }


}

